package client.tzstore.oo.dialog

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import client.comm.commlib.R
import client.comm.commlib.getScreenWidth

abstract class CenterDialog<V : ViewDataBinding> : Dialog {

    constructor(context: Context) : super(context, R.style.BottomDialog)
    constructor(context: Context, theme: Int) : super(context, theme)

    lateinit var binding: V
    lateinit var view: View

    abstract fun getV(): Int

    open fun postBinding() {}
    var widthMode = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = DataBindingUtil.inflate<V>(LayoutInflater.from(context), getV(), null, false) as V

        setContentView(binding.root)
        postBinding()

        if (widthMode == 0)
            window?.apply {
                setGravity(Gravity.CENTER)
                val width = getScreenWidth(context)
                setLayout(
                    width * 4 / 5,
                    WindowManager.LayoutParams.WRAP_CONTENT
                )
            }

    }
}